package LinkStack

//链式栈

type Node struct {
	Data  interface{}
	Pnext *Node
}

type LinkStack interface {
	IsEmpty() bool //判断栈是否为空
	Push(data interface{})
	Pop() interface{}
	Length() int
}

func NewLinkStack() LinkStack  {
	return &Node{}
}

func (node *Node) IsEmpty() bool {
	if node.Pnext == nil {
		return true
	} else {
		return false
	}
}

func (node *Node) Push(data interface{}) {
	newnode := &Node{data, nil}
	newnode.Pnext = node.Pnext
	node.Pnext = newnode
}

func (node *Node) Pop() interface{} {
	if node.IsEmpty() {
		return nil
	}
	value := node.Pnext.Data      //要弹出的数据
	node.Pnext = node.Pnext.Pnext //删除
	return value
}

func (node *Node) Length() int {
	pNext:= node
	length:=0
	for pNext.Pnext!=nil  {
		pNext = pNext.Pnext //节点循环跳跃
		length++
	}
	return length
}
